// UeResbuf.cpp: implementation of the UeResbuf class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "UeResbuf.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UeResbuf::UeResbuf()
{
	this->rbnext = NULL;
	this->restype = RTNONE;
	memset(&resval, 0, sizeof(resval));
}

UeResbuf::UeResbuf(struct resbuf* pRb)
{
	*this = pRb;
}

UeResbuf::~UeResbuf()
{
	if(this->restype == AcDb::kDxfXdAsciiString ||
		this->restype == AcDb::kDxfRegAppName)
		free(this->resval.rstring);
	if(this->rbnext != NULL)
		delete this->rbnext;
}

UeResbuf& UeResbuf::operator=(struct resbuf& buf)
{
	if(this == (UeResbuf*)(&buf))
		return *this;

	memcpy(this, &buf, sizeof(struct resbuf));
	if(buf.restype == AcDb::kDxfXdAsciiString ||
		buf.restype == AcDb::kDxfRegAppName)
	{
		INT nStrLen = strlen(buf.resval.rstring);
		this->resval.rstring = (char *)malloc(nStrLen + 1);
		memcpy(this->resval.rstring, buf.resval.rstring, nStrLen);
		this->resval.rstring[nStrLen] = '\0';
	}
	this->rbnext = NULL;

	return *this;
}

UeResbuf& UeResbuf::operator=(struct resbuf* buf)
{
	ASSERT(buf);
	*this = *buf;
	UeResbuf* pPrev = this;
	buf = buf->rbnext;
	while(buf != NULL)
	{
		UeResbuf* pBuf = new UeResbuf;
		*pBuf = *buf;
		pPrev->rbnext = pBuf;
		pPrev = pBuf;
		buf = buf->rbnext;
	}
	acutRelRb(buf);

	return *this;
}

void UeResbuf::Add(struct resbuf* buf)
{
	if(this->rbnext == NULL)
	{
		this->rbnext = (UeResbuf*)buf;
	}
	else
	{
		this->rbnext->Add(buf);
	}
}

void UeResbuf::Add(struct resbuf& buf)
{
	if(this->rbnext == NULL)
	{
		UeResbuf* pBuf = new UeResbuf;
		*pBuf = buf;
		this->rbnext = pBuf;
	}
	else
	{
		this->rbnext->Add(buf);
	}
}